//
//  HShowALLQuesView.m
//  NursingManages
//
//  Created by Zhen Huang on 17/9/22.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HShowALLQuesView.h"
#import "HShowQuesCollectionViewCell.h"
#import "Model_Hr_Question_Item.h"
#import "Model_Hr_ExamAnswer.h"
@implementation HShowALLQuesView

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray new];
        
        [self setUpLayOutCollection];
        
        self.cellDic = [[NSMutableDictionary alloc] init];

        [self.quesCollectView reloadData];
    }
    return self;
}

#pragma mark -- 对集合视图进行布局
- (void)setUpLayOutCollection {
    
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayOut.itemSize = CGSizeMake(50, 50);

    flowLayOut.minimumInteritemSpacing = 3;

    flowLayOut.minimumLineSpacing = 5;

    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
    

    flowLayOut.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.quesCollectView = [[UICollectionView alloc]initWithFrame:
                                            CGRectMake(0, 0, ScreenWidth, ScreenHeight - 250) collectionViewLayout:flowLayOut];
    self.quesCollectView.backgroundColor = [UIColor blueColor];

    self.quesCollectView.dataSource = self;
    
    self.quesCollectView.delegate = self;
    
    self.quesCollectView.backgroundColor = RGB(227, 227, 227);
    
    self.quesCollectView.backgroundColor = [UIColor whiteColor];
    
    [self.quesCollectView registerClass:[HShowQuesCollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    
    [self addSubview:self.quesCollectView];
    
    
}
#pragma mark -- 单元格的数量
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count ;
    
}

#pragma mark -- 配置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifier = [_cellDic objectForKey:[NSString stringWithFormat:@"%@", indexPath]];
    
    NSString *DayCell = @"cell";
    
    if (identifier == nil) {
        
        identifier = [NSString stringWithFormat:@"%@%@", DayCell, [NSString stringWithFormat:@"%@", indexPath]];
        
        [_cellDic setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]];
        
        [self.quesCollectView registerClass:[HShowQuesCollectionViewCell class]  forCellWithReuseIdentifier:identifier];
    }
    
    HShowQuesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    Model_Hr_Question_Item *model = self.dataArray[indexPath.row];
    
    Model_Hr_ExamAnswer *answerModel = self.examArray[indexPath.row];
    
    if (self.type == 1) {
        
        if (self.isTestOrExam == 2) {
            
            if (answerModel.RES_ANSWER == nil) {
                
                model.answerState = 1;
                
            }else {
                
                if (answerModel.RIGHT_FLAG == 1) {
                    
                    model.answerState = 2;
                    
                }else {
                    model.answerState = 3;
                }
            }

        }else {
            
            if (self.onlyTest == 0) {
                
                if (answerModel.RES_ANSWER == nil) {
                    
                    model.answerState = 1;
                    
                }else {
                    
                    if (answerModel.RIGHT_FLAG == 1) {
                        
                        model.answerState = 2;
                        
                    }else {
                        model.answerState = 3;
                    }
  
                }
            }else {
                
                if (model.answers == nil) {
                    
                    model.answerState = 1;
                    
                }else {
                    
                    if ([model.answers isEqualToString:model.ANSWER]) {
                        
                        model.answerState = 2;
                        
                    }else {
                        model.answerState = 3;
                    }
                    
                }
            }
 
        }

    }else {
        
        if (self.isTestOrExam == 2) {
            
            if (answerModel.RES_ANSWER == nil) {
                
                model.answerState = 1;
            }else {
                
                model.answerState = 2;
            }

        }else {
            
            if (model.answers == nil) {
                
                model.answerState = 1;
            }else {
                
                model.answerState = 2;
            }

        }
    }
    [cell configureWithModel:model type:self.type];

    cell.quesButton.tag = indexPath.row ;

    [cell.quesButton setTitle:[NSString stringWithFormat:@"%ld",indexPath.row + 1] forState:UIControlStateNormal];

    [cell.quesButton addTarget:self action:@selector(showQues:) forControlEvents:UIControlEventTouchUpInside];

    return  cell;
}

- (void)showQues:(UIButton *)sender {
    
    UIView *view = [self viewWithTag:1000];
    
    __weak HShowALLQuesView *weakSelf = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        view.frame = CGRectMake(0, self.bounds.size.height,self.bounds.size.width, 280);
        
    } completion:^(BOOL finished) {
        
        [weakSelf removeFromSuperview];
    }];
    self.indexPath = sender.tag;

    
    if (self.delegate && [self.delegate respondsToSelector:@selector(passValueByDelegateWithString:)]) {
        
        [self.delegate passValueByDelegateWithString:self.indexPath];
        
    }

}

- (NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
    
}


@end
